#include<stdio.h>
#include<unistd.h>

/*
before fork 
This is father process
pid=28489
father after fork
This is child process
pid=0
child after fork
	1. 父进程拿到的fork函数返回值是子进程的进程号
	2. 子进程只执行fork之后的代码,后续用到的pid就用0表示
	3. 父子进程执行顺序是操作系统决定的
 *
 *
 */

int main(){
	pid_t pid;
	printf("before fork \n");
	pid = fork();
	if(pid > 0){
		printf("This is father process\n");
		printf("pid=%d\n", (int)pid);
		printf("father after fork\n");

		while(1){
			sleep(1);
			printf("father sleep\n");
		}

	}else if(pid == 0){
		printf("This is child process\n");
		printf("pid=%d\n", (int)pid);
		printf("child after fork\n");
		
		while(1){
			sleep(1);
			printf("child sleep\n");
		}

	}else{
		perror("fork");
	}

}
